import functools
import json
import os
from datetime import datetime
import re
import pymongo
from logzero import logger
from selenium import webdriver
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from io import BytesIO
from PIL import Image
import requests
from config import USE_FACE, CHROME_DRIVER_PATH, PHANTOMJS_PATH, SCREENSHOT_PATH, MONGO_HOST, MONGO_PORT, \
    MONGO_DATABASE, RUN_END, RUN_BEGIN, DEBUG
from send_email import sendmail


class open_driver(object):
    def __init__(self, width=1920, height=7000, cookie_domain=None, load_image=False):
        self.width = width
        self.height = height
        self.cookie_domain = cookie_domain
        self.load_image = load_image
        if self.cookie_domain:
            self.cookie_file = os.path.join(os.path.expanduser('~'), self.cookie_domain[1:] + '.json')
            if not os.path.exists(self.cookie_file):
                raise Exception(f'{self.cookie_file} 不存在，请设置')

    def __enter__(self):
        if USE_FACE:
            self.driver = webdriver.Chrome(CHROME_DRIVER_PATH)
            # self.driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver')

            logger.info('chrome浏览器打开')
            self.driver.get('http://www.baidu.com')
        else:
            dcap = dict(DesiredCapabilities.PHANTOMJS)
            dcap["phantomjs.page.settings.userAgent"] = (
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"
            )
            service_args = []

            if not self.load_image:
                service_args.append('--load-images=no')
            service_args.append('--disk-cache=yes')
            service_args.append('--ignore-ssl-errors=true')
            self.driver = webdriver.PhantomJS(PHANTOMJS_PATH, desired_capabilities=dcap, service_args=service_args)
            logger.info('phantomjs浏览器打开')
        self.driver.set_window_size(self.width, self.height)
        self.add_cookie()
        js = "window.scrollTo(0, document.body.scrollHeight)"
        self.driver.scroll_buttom = lambda: self.driver.execute_script(js)
        return self.driver

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_tb:
            self.driver.get_screenshot_as_file(
                f"{SCREENSHOT_PATH}/excep_{datetime.now().strftime('%Y-%m-%d %H %M %S')}.png")
        self.store_cookie()
        logger.info("浏览器关闭")
        self.driver.close()
        self.driver.quit()
        if exc_tb:
            logger.error("出现异常")
            logger.error(exc_type)
            logger.error(exc_val)
            logger.error(exc_tb)
            return False

    def add_cookie(self):
        if not self.cookie_domain:
            return
        logger.info('加载cookie')
        self.driver.delete_all_cookies()
        cookies = json.load(open(self.cookie_file, 'r'))
        for c in cookies:
            if c.get('domain') != self.cookie_domain:
                continue
            self.driver.add_cookie(c)
        logger.info('加载完成')

    def store_cookie(self):
        if not self.cookie_domain:
            return
        logger.info('存储cookie')
        new_cookies = self.driver.get_cookies()
        json.dump(new_cookies, open(self.cookie_file, 'w'))
        logger.info('加载完成')


class track_alert(object):
    def __init__(self, driver):
        self.driver = driver

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type == UnexpectedAlertPresentException:
            if exc_tb:
                self.driver.get_screenshot_as_file(
                    f"{SCREENSHOT_PATH}/alert_{datetime.now().strftime('%Y-%m-%d %H %M %S')}.png")
            import re
            msg = re.findall(r'{Alert text : (.*)}', exc_val.msg)[0]
            logger.error(msg)
            return True


global_connection = None


def get_connection():
    global global_connection
    if not global_connection:
        global_connection = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
    return global_connection


def get_mongo_collection(collection_name):
    connection = get_connection()
    database = connection[MONGO_DATABASE]
    return database[collection_name]


def get_collection_names():
    connection = get_connection()
    database = connection[MONGO_DATABASE]
    return database.collection_names()


def cookie_from_chrome_to_json(cookie_str, domain):
    cookie_list = cookie_str.split(';')
    cookies = []
    for c in cookie_list:
        name, value = c.split('=', maxsplit=1)
        cookie = {'domain': domain, 'name': name, 'value': value, 'path': '/'}
        cookies.append(cookie)
    cookie_file = os.path.join(os.path.expanduser('~'), domain[1:] + '.json')
    json.dump(cookies, open(cookie_file, 'w'))
    logger.info(f'持久化{cookie_file}')


def record_run_status(module):
    """
    记录运行的起止时间和新增条数
    :param module:
    :return:
    """

    def outer(func):
        @functools.wraps(func)
        def inner(*args, **kwargs):
            COLLECTION_NAME = 'record_run_status'
            db = get_mongo_collection(COLLECTION_NAME)
            begin_time = datetime.now()
            data = {'module': module, 'status': RUN_BEGIN, 'update_time': begin_time}
            db.insert_one(data)
            insert_count = func(*args, **kwargs)
            end_time = datetime.now()
            data = {'module': module, 'status': RUN_END, 'insert_count': insert_count, 'update_time': end_time}
            db.insert_one(data)
            # env = '测试环境' if DEBUG else '生产环境'
            # email_content = f"""
            # <h3> 模块{module} 运行<br>开始于 {begin_time},<br> 结束于 {end_time}, <br>运行时间 {end_time - begin_time}, <br>新增数据 {insert_count}条</h3>
            # """
            # sendmail(content=email_content, subject=f'轻点爬虫通知({env})', attachments=[], nick_from='爬虫蜘蛛')
            return insert_count

        return inner

    return outer

def re_html_tag(html):
    mp = re.compile(r'<span (.*)>(.*)视频', re.S)
    html = mp.sub('', html)
    dr = re.compile(r'<[^>]+>', re.S)
    dd = dr.sub('', html)
    return dd
def get_img_url_wh(img_url):
    img=Image.open(BytesIO(requests.get(img_url).content))
    return img.size

@record_run_status('utils')
def ff():
    print('ff')


if __name__ == '__main__':
    print(CHROME_DRIVER_PATH)
    print(SCREENSHOT_PATH)
    # COLLECTION_NAME = 'feed'
    # db = get_mongo_collection(COLLECTION_NAME)
    # from bilibili_submitvideo_list import get_feeds
    #
    # feeds = get_feeds()
    # for mid in feeds:
    #     data = {'purpose': 'B站UP主', 'mid': mid}
    #     db.insert_one(data)
    # logger.info('插入B站UP主完成')
    # from lofter import get_feeds
    #
    # feeds = get_feeds()
    # for user_name, name, uid in feeds:
    #     data = {'purpose': '乐乎', 'user_name': user_name, 'name': name, 'uid': uid}
    #     db.insert_one(data)
    # logger.info('插入乐乎完成')
    # from weibo_cos import get_feeds
    #
    # feeds = get_feeds()
    # for feed in feeds:
    #     type_ = feed['type']
    #     name = feed['name']
    #     cos = feed['cos']
    #     uid = feed['uid']
    #     data = {'purpose': '微博', 'type_': type_, 'name': name, 'cos': cos, 'uid': uid}
    #     db.insert_one(data)
    # logger.info('插入微博完成')
    # from zhihu_zhuanlan import get_feeds
    #
    # feeds = get_feeds()
    # for user_name, name in feeds:
    #     data = {'purpose': '知乎专栏', 'user_name': user_name, 'name': name}
    #     db.insert_one(data)
    # logger.info('插入知乎专栏完成')
    # cookie_str = 'uuid="w:cded8c27813a4eeaa3a83afc1163dbbb"; UM_distinctid=1602b487f542d-03adc89c08cce4-5b44271d-1fa400-1602b487f55817; sso_login_status=1; login_flag=fa94a177f28e705bbcd553411ac730d3; sessionid=eb619c7cb92026e1003a24c5c8c31e56; uid_tt=fd6d6df3470bd75ec18975eeb3032d33; sid_tt=eb619c7cb92026e1003a24c5c8c31e56; sid_guard="eb619c7cb92026e1003a24c5c8c31e56|1512982918|15552000|Sat\054 09-Jun-2018 09:01:58 GMT"; _ga=GA1.2.251321692.1512554660; tt_webid=6496372805708793358; WEATHER_CITY=%E5%8C%97%E4%BA%AC; __tasessionId=kxkubitoh1513840065753; CNZZDATA1259612802=630978241-1512552895-https%253A%252F%252Fwww.baidu.com%252F%7C1513836009; tt_webid=6496372805708793358'
    # cookie_from_chrome_to_json(cookie_str, '.toutiao.com')
    #     data_str = """最爱二次元 	6160472582
    # 漫动社 	1578698210611213
    # 动漫看看	1577658727379981
    # 小迪动漫	1560935902517250
    # 二次元漫宅	1575981921569806
    # 图图杂货铺	1581204176001037
    # 过吹过的夏天	1568916056784897
    # 爱上苦的咖啡	1560941699887105
    # 动漫图片软妹	1571903038589953
    # 动漫温馨基地	7002028925
    # 自由之羽翼	1573787847329805
    # 回到过去流年	1560208756997122"""
    #     db = get_mongo_collection('feed')
    #     db.remove({'purpose': '头条图片'})
    #     data_list = []
    #     for d in data_str.split('\n'):
    #         user_name, uid = d.split('\t')
    #         data = {'purpose': '头条图片', 'uid': uid, 'user_name': user_name}
    #         data_list.append(data)
    #
    #     db.insert_many(data_list)
    #     data_str = """cos博主	W小小白W	cos博主	1732676734
    # cos博主	AC爱丽丝伪娘团	cos博主	2139358713
    # cos博主	软妹料理姬	cos博主	2994600662
    # cos博主	昭君TOUCH	cos博主	2667595283
    # cos博主	KumaQi熊祁	cos博主	2294060951
    # cos博主	猫梓子	cos博主	1717701417
    # cos博主	Kilory_莉莉	cos博主	1797491625
    # cos博主	千夜未来	cos博主	1894225741
    # cos博主	夕楼76	cos博主	1799599962
    # cos博主	我是cosplayer	cos博主	1878972254
    # cos博主	矮乐多Aliga	cos博主	2711388952
    # cos博主	azusawww	cos博主	6041027272
    # cos博主	狗司令太子 	cos博主	1797753301
    # cos博主	一之瀨光	cos博主	1785393560
    # cos博主	coser小梦	cos博主	1766909504
    # cos博主	行云	cos博主	1201834052
    # cos博主	岚陵萧萧声EX	cos博主	1685802840
    # cos博主	桃昔doll 	cos博主	1996669711
    # cos博主	摄影9课官方微薄	cos博主	2159013314"""
    #     db = get_mongo_collection('feed')
    #     data_list = []
    #     for d in data_str.split('\n'):
    #         type_, name, cos, uid = d.split('\t')
    #         data = {'purpose': '微博', 'type_': type_, 'name': name, 'cos': cos, 'uid': uid}
    #         data_list.append(data)
    #         logger.info(f'插入{data}')
    #
    #     db.insert_many(data_list)
    # ff()
